<template>
	<view>
		<!-- 搜索框 -->
		<view class="search-box">
			<uni-search-bar placeholder="请输入搜索内容" @confirm="search" @input="input" :radius="100" cancelButton="none"
				clearButton="auto" :focus="true"></uni-search-bar>
		</view>
		<!-- 搜索列表展示区 -->
		<view class="sugg-list" v-if="searchResults.length !== 0">
			<view class="sugg-item" v-for="item in searchResults" :key="item.goods_id" @click="gotoDetail(item.goods_id)">
				<view class="goods-name">{{item.goods_name}}</view>
				<uni-icons type="forward" size="16"></uni-icons>     
			</view>
		</view>
		<!-- 搜索历史区域 -->
		<view class="history-box" v-else>
			<!-- 搜索标题 -->
			<view class="history-title">
				<text>搜索历史</text>
				<uni-icons type="trash-filled" size="17" @click="clean"></uni-icons>
			</view>
			<!-- 历史列表 -->
			<view class="history-list">
				<uni-tag :text="item" 
				v-for="(item,i) in historys" 
				:key="i" 
				@click="gotoGoodsList(item)"></uni-tag>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				timer: null,
				// 搜索关键词
				kw:'',
				// 搜索结果数组
				searchResults:[],
				// 历史关键词
				historyList:[]
			};
		},
		computed:{
			historys(){
				return [...this.historyList].reverse()
			}
		},
		methods: {
			// 输入事件处理函数 
			input(e) {
				//防抖处理
				clearTimeout(this.timer)
				// 启动定时器
				this.timer = setTimeout(() => {
					this.kw = e
					this.getSearchList()
				}, 500)
			},
			// 获取搜索数据
			async getSearchList(){
				if(this.kw == ''){
					this.searchResults = []
					return
				}
				const res =  await uni.$http.get('/api/public/v1/goods/qsearch?query='+this.kw)
				 
				if(res.data.meta.status == 200){
					this.searchResults = res.data.message
				}
				// 查询到搜索建议后，保存关键字
				this.saveSearchHistory()
			},
			// 点击搜索项跳转详情页
			gotoDetail(goods_id){
				uni.navigateTo({
					url:'/subpkg/goods-detail/goods-detail?goods_id='+ goods_id
				})		
			},
			// 保存关键字到历史
			saveSearchHistory(){
				const set = new Set(this.historyList)
				set.delete(this.kw)
				set.add(this.kw)
				
				this.historyList = Array.from(set)
				// 将历史记录持久化存储到本地
				uni.setStorageSync('kw',JSON.stringify(this.historyList))
			},
			// 清除历史记录
			clean(){
				this.historyList = []
				uni.setStorageSync('kw','[]')
			},
			// 点击历史记录跳转到商品列表
			gotoGoodsList(kw){
				uni.navigateTo({
					url:'/subpkg/goods-list/goods-list?query='+kw
				})
			}
			
		},
		onLoad() {
			this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]')
		}
	}
</script>

<style lang="scss">
	.search-box {
		background-color: #c00000;
		height: 50px;
		position: sticky;
		top: 0;
		z-index: 99;
	}
	.sugg-list{
		padding: 0 5px;
			
		.sugg-item{
			font-size: 12px;
			padding: 13px 0 ;
			border-bottom: 1px solid #efefef;
			display:flex;
			align-items: center;
			justify-content: space-between;
			
			.goods-name{
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
				margin-right: 3px;
			}
		}
	}
	.history-box{
		padding: 0 5px;
		.history-title{
			display: flex;
			justify-content: space-between;
			height: 40px;
			align-items: center;
			font-size: 13px;
			border-bottom: 1px solid #efefef;
		}
		.history-list{
			display: flex;
			flex-wrap: wrap;
			.uni-tag{
				background-color: #ebebeb;
				border: none;
				color: black;
				margin-top: 5px;
				margin-right: 5px;
				
			}
		}
	}
</style>
